clear all
set more off
cap log close
***************************************************************************************************
* Program: analysis_appendix.do
* Purpose: Produce figures and tables in the appendix
* Sections:
*     1. Appendix Figure: Figure A1-Figure A9
*     2. Appendix Table: Table A1-Table A7
*     3. Statistics in the paper 
* Files Used:
*     1. phones_LEMAS_LEOKA_validate.dta
*     2. N_officers_NYPD_by_zipcode.dta
*     3. NYPDFoilOfficerHomeZipData_long.dta
*     4. police_hour_by_shift.dta
*     5. acs13-17_bg_demographics.dta
*     6. police_hour_data_for_reg.dta
*     7. bg_stop_arrest_per_hour_for_reg.dta
*     8. 311_calls_bg_count_NYC_2016.dta
* Files Created:
*     1. Figure A1: 
*     2. Figure A2: 21cities_per_capita_patrol_phones_corr.png
*     3. Figure A3: scatter_police_race_share_combined.png
*     4. Figure A4: bs_arsinh_NYPD_officer_counts.png
*     5. Figure A5: bs_asinh_arrest_hour_6cities.png
*     6. Figure A6: bs_asinh_stop_hour_9cities.png
*     7. Figure A7: coefplot_raw_abs_black_disparity_by_shift_hr.png; coefplot_raw_abs_hisp_disparity_by_shift_hr.png
*     8. Figure A8: scatter_police_supervisor_black.png
*     9. Figure A9: coefplot_raw_black_by_city.png
*     10. Table A1: reg_LEMAS_comparison.tex
*     11. Table A2: summary_table.tex
*     12. Table A3: reg_13-16homicide_w_NYC_abs.tex
*     13. Table A4: reg_hour_311_calls_NYC_abs_race.tex
*     14. Table A5: asinh_hour_race_nonwork.tex
*     15. Table A6: asinh_hour_raw_race_NYC_4col.tex
*     16. Table A7: asinh_conditional_stops_abs_race.tex
* Notes:
*     Need to ssc install gtools, outreg2, sutex2, geoinpoly, geonear, binscatter if not already installed.
***************************************************************************************************

***************************************************************************************************
** 1. Appendix Figures
***************************************************************************************************
************************************************************************
** Figure A1
************************************************************************
/*
* Scatterplot for work/home/other patterns (Using LAPD)
use "${DataRaw}/17_LAPD_QualPhone_Pings.dta", clear

gen geo2 = substr(geo_hash, 1, 2)
keep if geo2 == "9q" |  geo2 == "9m"

sort Master_ID_PostFeb17 utc_timestamp
drop if utc_timestamp == .
drop horizontal_accuracy Apple_0_Google_1
gegen temp = tag(Master_ID_PostFeb17 utc_timestamp)
drop if temp == 0
drop temp

* Create a tag for pings in Police Stations *
merge 1:1 Master_ID_PostFeb17 utc_timestamp using "${DataRaw}/Pings_in_PD_for_Qualified_Phones.dta", keepusing(city station Building_ID_new) keep(match master) nogenerate
gen byte match_PD = (Building_ID_new != .)

* Create a tag for pings at Home *
gen int StataDate_UTC = dofc((utc_timestamp * 1000) + mdyhms(1,1,1970,0,0,0))
format StataDate_UTC %td
gen Month = month(StataDate_UTC)

merge m:1 Master_ID_PostFeb17 using "${DataRaw}/Qualified_Phones_Imputed_Homes.dta", keep(master match) keepusing(Home_Geo7_Early Home_Geo7_Late Home_Geo7_Year) nogenerate

gen Geohash_7 = substr(geo_hash, 1, 7)
gen Home_Geo7 = Home_Geo7_Early
replace Home_Geo7 = Home_Geo7_Late if Month > 6
replace Home_Geo7 = Home_Geo7_Year if Home_Geo7 == ""
gen byte match_Home = (Geohash_7 == Home_Geo7)

drop Home_Geo7_*
compress

gen HourOfDay_Local = hh(Local_Time) 

**************************************************
** keep only one ping in every 10 minute block **
**************************************************
* drop these two variables because some pings are deleted
drop Sec_Since_Last_Ping Sec_Till_Next_Ping

* groups of pings 
gen group_10min = mm(Local_Time)
replace group_10min  = floor(group_10min/10)*10 

egen ID_10min_block = group(Master_ID_PostFeb17 StataDate_Local HourOfDay_Local group_10min)
bys ID_10min_block: gegen double med_local_Time = median(Local_Time)
bys ID_10min_block: gegen double med_utc_timestamp = median(utc_timestamp)
bys ID_10min_block: gegen double med_local_timestamp = median(local_timestamp)
bys ID_10min_block: gen double med_lat_ = latitude if med_local_Time == Local_Time
bys ID_10min_block: gen double med_lng_ = longitude if med_local_Time == Local_Time
sort ID_10min_block Local_Time
** for 10 min block with even number of pings -> define median lat long as lat / long for the earlier ping 
by ID_10min_block: replace med_lat_ = latitude if Local_Time < med_local_Time & Local_Time[_n+1] > med_local_Time
by ID_10min_block: replace med_lng_ = longitude if Local_Time < med_local_Time & Local_Time[_n+1] > med_local_Time
bys ID_10min_block: gegen double med_lat = max(med_lat_) // fill in missing value
bys ID_10min_block: gegen double med_lng = max(med_lng_) // fill in missing value 
format med_local_Time %tc

drop med_lat_ med_lng_

bys ID_10min_block: gegen max_in_PD = max(in_PD) // see whether there are in_PD pings during this 10 minutes period
bys ID_10min_block: gegen max_in_Home = max(match_Home) // see whether there are in_PD pings during this 10 minutes period

* collapse data
drop if ID_10min_block==ID_10min_block[_n-1]
keep med_* max* Building_ID Master_ID_PostFeb17 ID_10min_block 

gen StataDate_Local = dofc(med_local_Time)
gen Hour = hh(med_local_Time) + mm(med_local_Time)/60 
gen month = month(StataDate_Local)
gen day = day(StataDate_Local)

sort Master_ID_PostFeb17 StataDate_Local Hour
gegen day_in_PD = max(max_in_PD), by(Master_ID_PostFeb17 StataDate_Local)
keep if day_in_PD

* create mode (PD/Other/Home Geo7) * 
gen byte mode = 1 if max_in_PD == 1
replace mode = -1 if max_in_Home == 1
replace mode = 0 if missing(mode)

label define modelab 1 "Work" 0 "Other" -1 "Home"
label values mode modelab

format StataDate_Local %td

label define daylab 20940 "May 1" 20941 "May 2" 20942 "May 3" ///
	20943 "May 4" 20944 "May 5" 20945 "May 6" 20946 "May 7" 20947 "May 8" ///
	20948 "May 9" 20949 "May 10" 20950 "May 11" 20951 "May 12" ///
	20952 "May 13" 20953 "May 14" 20954 "May 15" 20955 "May 16"  ///
	20956 "May 17" 20957 "May 18" 20958 "May 19" 20959 "May 20" ///
	20960 "May 21" 20961 "May 22" 20962 "May 23" 20963 "May 24"  ///
	20964 "May 25" 20965 "May 26" 20966 "May 27" 20967 "May 28" ///
	20968 "May 29" 20969 "May 30" 20970 "May 31" 
label values StataDate_Local daylab

local IDs "799353" // ID in LAPD
foreach ID of local IDs{
tw scatter mode Hour if Master==`ID' & day_in_PD==1 & month==5, ///
	ylabel(-1(1)1, valuelabel noticks) by(StataDate_Local, note("") graphregion(color(white)) ) ///
	xlabel(0(2)22, labsize(small)) ytitle("Mode") msymbol(Oh) msize(small) mcolor(navy) scheme(s2color)
graph export "${Output}/mode_hour_ID_`ID'.png",replace width(2000)
}
*/

************************************************************************
** Figure A2: Comparison with LEOKA
************************************************************************

use "${DataCleaned}/phones_LEMAS_LEOKA_validate.dta", clear

********************************
* Figure A.1 (a) 
********************************
gen byte right_label = inlist(city, "INDIANAPOLIS", "SANANTONIO", "DENVER", "COLUMBUS", "FORTWORTH")
gen byte left_label = inlist(city, "SANFRANCISCO", "LOSANGELES")

corr N_patrol_phones_2017 total_employees_officers
local lincorr = round(r(rho),.001)
tw (scatter total_employees_officers N_patrol_phones_2017 if right_label == 0 & left_label ==0, ///
	scheme(s2color) graphregion(color(white)) yscale(log) xscale(log) ///
	mlabel(city2) mlabpo(2) msize(small) mlabsize(vsmall) mlabpo(12) mlabgap(*1) xlabel(200(2000)7000) ///
	ytitle("Total Officer Counts (UCR) (Log Scale)") xtitle("Patrol Phone Counts (Log Scale) ") ///
	text(1200 4500 "Corr: `lincorr'") xlabel(, labsize(small)) ylabel(, labsize(small)) ylabel(1000(15000)50000)) ///
	(scatter total_employees_officers N_patrol_phones_2017 if right_label == 1, ///
	mcolor(navy) mlabcolor(navy) mlabel(city2) mlabpo(2) msize(small) mlabsize(vsmall) mlabpo(3) mlabgap(*1)) ///
	(scatter total_employees_officers N_patrol_phones_2017 if left_label == 1, ///
	mcolor(navy) mlabcolor(navy) mlabel(city2) mlabpo(2) msize(small) mlabsize(vsmall) mlabpo(9) mlabgap(*1) legend(off))
graph export "${Output}/21cities_number_patrol_phones_corr.png",replace

********************************
* Figure A.1 (b)
********************************
gen per_cap_tot_officers = total_employees_officers / city_tot_pop_acs_13_17 * 100000
gen per_cap_N_patrol_phones_2017 = N_patrol_phones_2017 / city_tot_pop_acs_13_17 * 100000

gen byte left_label_per100k = (city == "NASHVILLE" )
gen byte down_label_per100k = inlist(city, "AUSTIN", "WASHINGTON")

corr  per_cap_N_patrol_phones_2017 per_cap_tot_officers
local lincorr_per100k = round(r(rho),.001)

tw (scatter per_cap_tot_officers per_cap_N_patrol_phones_2017 if left_label_per100k == 0 & down_label_per100k == 0, ///
	scheme(s2color) graphregion(color(white)) ///
	xlabel(, labsize(small)) ylabel(, labsize(small)) /// 
	ytitle("Total Officer Counts (UCR) (Per 100k)") xtitle("Patrol Phone Counts (Per 100k)") ///
	mlabel(city2) mlabsize(vsmall) mcolor(navy) mlabcolor(navy) msize(small) text(120 47 "Corr: `lincorr_per100k'")) ///
	(scatter per_cap_tot_officers per_cap_N_patrol_phones_2017 if left_label_per100k == 1,  ///
	mlabel(city2) mlabsize(vsmall) mcolor(navy) mlabcolor(navy) mlabpo(9) mlabgap(*1) msize(small)) ///
	(scatter per_cap_tot_officers per_cap_N_patrol_phones_2017 if down_label_per100k == 1,  ///
	mlabel(city2) mlabsize(vsmall) mcolor(navy) mlabcolor(navy) mlabpo(6) mlabgap(*1)  msize(small) legend(off))
graph export "${Output}/21cities_per_capita_patrol_phones_corr.png",replace

************************************************************************
** Figure A3: Comparison with LEMAS
************************************************************************

use "${DataCleaned}/phones_LEMAS_LEOKA_validate.dta", clear

********************************
* LEMAS: scatter plot 
********************************
* White
gen byte down_label_white = (city == "COLUMBUS" | city == "FORTWORTH" | city == "OKLAHOMACITY")
gen byte left_label_white = (city == "BOSTON" | city == "SANFRANCISCO")
	
qui reg police_pct_white phones_white_17, r		
local beta_white = round(_b[phones_white_17], .001)
local con_white = round(_b[_cons], .001)	
display "`con_white'"
tw (line linevar linevar, legend(off) scheme(s2color) lcolor(maroon) graphregion(color(white))) ///
	(scatter police_pct_white phones_white_17 if down_label_white == 0 & left_label_white == 0, ///
	mcolor(navy) mlabcolor(navy) mlabel(city2) mlabp(12) mlabsize(vsmall) msize(small) ///
	ytitle("Police: % White") xtitle("Smartphone: % White") ///
	text(0.05 0.8 "Intercept: `con_white', Slope: `beta_white'")) ///
	(scatter police_pct_white phones_white_17 if down_label_white == 1, ///
	mcolor(navy) mlabcolor(navy) mlabel(city2) mlabp(6) mlabsize(vsmall) msize(small)) ///
	(scatter police_pct_white phones_white_17 if left_label_white == 1, ///
	mcolor(navy) mlabcolor(navy) mlabel(city2) mlabp(9) mlabsize(vsmall) msize(small)) 
graph save "${Output}/scatter_police_white_share.gph",replace 

* Black
gen city_black = city2
replace city_black = "" if phones_black_17<0.13 
qui reg police_pct_black phones_black_17, r		
local beta_black = round(_b[phones_black_17], .001)
local con_black = round(_b[_cons], .001)
tw (line linevar linevar , legend(off) scheme(s2color) lcolor(maroon) graphregion(color(white))) ///
	(scatter police_pct_black phones_black_17, mlabel(city_black) mlabp(3) mlabsize(vsmall) msize(small) mcolor(navy) mlabcolor(navy) ///
	ytitle("Police: % Black") xtitle("Smartphone: % Black") text(0.05 0.8 "Intercept: `con_black', Slope: `beta_black'"))
graph save "${Output}/scatter_police_black_share.gph",replace 

* Hispanic
qui reg police_pct_hisp phones_hisp_17, r
gen byte down_label_hisp = (city == "COLUMBUS" | city == "FORTWORTH" )
gen byte left_label_hisp = (city == "SANFRANCISCO" | city == "DENVER" | city == "SANFRANCISCO" | city == "DETROIT")
gen byte no_label_hisp = (city == "OKLAHOMACITY" | city == "WASHINGTON" | city == "PHILADELPHIA")

local beta_hisp = round(_b[phones_hisp_17], .001)
local con_hisp = round(_b[_cons], .001)	
tw (line linevar linevar , legend(off) scheme(s2color) lcolor(maroon) graphregion(color(white))) ///
	(scatter police_pct_hisp phones_hisp_17 if down_label_hisp == 0 & left_label_hisp == 0 & no_label_hisp == 0, ///
	mlabel(city2) mlabp(3) mlabsize(vsmall) msize(small) mcolor(navy) mlabcolor(navy) ///
	ytitle("Police: % Hispanic") xtitle("Smartphone: % Hispanic") text(0.05 0.8 "Intercept: `con_hisp', Slope: `beta_hisp'")) ///
	(scatter police_pct_hisp phones_hisp_17 if down_label_hisp==1, ///
	mlabel(city2) mlabp(5) mlabsize(vsmall) msize(small) mcolor(navy) mlabcolor(navy)) ///
	(scatter police_pct_hisp phones_hisp_17 if left_label_hisp==1, ///
	mlabel(city2) mlabp(9) mlabsize(vsmall) msize(small) mcolor(navy) mlabcolor(navy)) ///
	(scatter police_pct_hisp phones_hisp_17 if no_label_hisp==1, ///
	mlabp(9) mlabsize(vsmall) msize(small) mcolor(navy))
graph save "${Output}/scatter_police_hisp_share.gph",replace

* Asian
qui reg police_pct_asian phones_asian, r		
local beta_asian = round(_b[phones_asian], .001)
local con_asian = round(_b[_cons], .001)
tw (line linevar linevar, legend(off) lcolor(maroon) scheme(s2color) graphregion(color(white))) ///
	(scatter police_pct_asian phones_asian,  mcolor(navy) mlabp(12) mlabsize(vsmall) msize(small) ///
	ytitle("Police: % Asian") xtitle("Smartphone: % Asian") text(0.05 0.8 "Intercept:`con_asian', Slope: `beta_asian'")) 
graph save "${Output}/scatter_police_asian_share.gph",replace

* combine graphs
cd "${Output}/"
graph combine scatter_police_white_share.gph scatter_police_black_share.gph ///
	scatter_police_hisp_share.gph  scatter_police_asian_share.gph , ///
	 row(2) ysize(16) xsize(20)  iscale(*.7)
graph export "${Output}/scatter_police_race_share_combined.png",replace width(2000)	

* remove intermediate output 
local filelist: dir "${Output}/" files "scatter_police_*_share.gph"
clear
foreach f of local filelist{
 rm "${Output}/`f'"
}

************************************************************************
** Figure A4
************************************************************************

use "${master_dir}/DataRaw/N_officers_NYPD_by_zipcode.dta",clear
merge 1:1 zipcode_str using "${DataRaw}/NYPDFoilOfficerHomeZipData_long.dta"

* fill in zeros
replace N_officer = 0 if N_officer==. 
replace N_device_Year = 0 if N_device_Year==.

* transform variables 
gen double asinh_N_device_Year = asinh(N_device_Year)
gen double asinh_N_officer = asinh(N_officer)

* make binscatter plot
corr asinh_N_device_Year asinh_N_officer if _merge>1
local lincorr = round(r(rho),.01)	
binscatter asinh_N_device_Year asinh_N_officer if _merge>1, scheme(s2color) ///
	xtitle("arsinh(Number of NYPD Officer)") ///
	ytitle("arsinh(Number of Smartphone)") text(0.25 5 "Corr.: `lincorr'")   
graph export "${Output}/bs_arsinh_NYPD_officer_counts.png", replace

************************************************************************
** Figure A5
************************************************************************

use "${DataCleaned}/bg_stop_arrest_per_hour_for_reg.dta", clear
	
******
* Binscatter plot by city: asinh_bg_arrest_count and asinh_hour_in_bg
******
cd "${Output}/"

** LOS ANGELES ** 
corr asinh_bg_arrest_count asinh_hour_in_bg if city=="LOSANGELES"
local asinhcor_arr = round(r(rho),.01)
binscatter asinh_bg_arrest_count asinh_hour_in_bg if city=="LOSANGELES", ///
	 text(.25 5 "Corr.: `asinhcor_arr'") scheme(s2color) ///
	 title("Los Angeles") ytitle("") xtitle("") replace ylabel(0(1)5) xlabel(0(1)6) ///
	 savegraph("bs_asinh_arrest_hour_LOSANGELES.gph")

** NEWYORKCITY **
corr asinh_bg_arrest_count asinh_hour_in_bg if city=="NEWYORKCITY"
local asinhcor_arr = round(r(rho),.01)
binscatter asinh_bg_arrest_count asinh_hour_in_bg if city=="NEWYORKCITY", ///
	text(.25 5 "Corr.: `asinhcor_arr'") scheme(s2color) ///
	 title("New York City") ytitle("") xtitle("") replace ylabel(0(1)5) xlabel(0(1)6) ///
	 savegraph("bs_asinh_arrest_hour_NEWYORKCITY.gph")
	 
** CHICAGO **
corr asinh_bg_arrest_count asinh_hour_in_bg if city=="CHICAGO"
local asinhcor_arr = round(r(rho),.01)
binscatter asinh_bg_arrest_count asinh_hour_in_bg if city=="CHICAGO", ///
	 text(.25 5 "Corr.: `asinhcor_arr'") scheme(s2color) ///
	 title("Chicago") ytitle("") xtitle("") replace ylabel(0(1)5) xlabel(0(1)6) ///
	 savegraph("bs_asinh_arrest_hour_Chicago.gph")

** AUSTIN **
corr asinh_bg_arrest_count asinh_hour_in_bg if city=="AUSTIN"
local asinhcor_arr = round(r(rho),.01)
binscatter asinh_bg_arrest_count asinh_hour_in_bg if city=="AUSTIN", ///
	 text(.25 5 "Corr.: `asinhcor_arr'") scheme(s2color) ///
	 title("Austin") ytitle("") xtitle("") replace ylabel(0(1)5) xlabel(0(1)6) ///
	 savegraph("bs_asinh_arrest_hour_AUSTIN.gph")

** DALLAS ** 
corr asinh_bg_arrest_count asinh_hour_in_bg if city=="DALLAS"
local asinhcor_arr = round(r(rho),.01)
binscatter asinh_bg_arrest_count asinh_hour_in_bg if city=="DALLAS", ///
	 text(.25 5 "Corr.: `asinhcor_arr'") scheme(s2color) ///
	 title("Dallas") ytitle("") xtitle("") replace ylabel(0(1)5) xlabel(0(1)6) ///
	 savegraph("bs_asinh_arrest_hour_DALLAS.gph")

** WASHINGTON ** 
corr asinh_bg_arrest_count asinh_hour_in_bg if city=="WASHINGTON"
local asinhcor_arr = round(r(rho),.01)
binscatter asinh_bg_arrest_count asinh_hour_in_bg if city=="WASHINGTON", ///
	 text(.25 5 "Corr.: `asinhcor_arr'") scheme(s2color) ///
	 title("Washington") ytitle("") xtitle("") replace ylabel(0(1)5) xlabel(0(1)6) ///
	 savegraph("bs_asinh_arrest_hour_WASHINGTON.gph")
	 
* Combine graphs
graph combine bs_asinh_arrest_hour_NEWYORKCITY.gph bs_asinh_arrest_hour_LOSANGELES.gph bs_asinh_arrest_hour_Chicago.gph ///
	bs_asinh_arrest_hour_WASHINGTON.gph bs_asinh_arrest_hour_DALLAS.gph bs_asinh_arrest_hour_AUSTIN.gph, ///
	row(3) ysize(20) xsize(16) iscale(*.7)  ///
	b1("arsinh(Hour in BG)", size(small)) l1("arsinh(Number of Arrests)", size(small))  	
graph export "bs_asinh_arrest_hour_6cities.png",replace  width(4000)

local filelist: dir "${Output}/" files "bs_asinh_arrest_hour_*.gph"
clear
foreach f of local filelist{
 rm "${Output}/`f'"
}
************************************************************************
** Figure A6
************************************************************************

use "${DataCleaned}/bg_stop_arrest_per_hour_for_reg.dta", clear

cd "${Output}/"
*****
* Binscatter plot by city: asinh_bg_stop_count and asinh_hour_in_bg
*****
** DENVER **
corr asinh_bg_stop_count asinh_hour_in_bg if city=="DENVER"
local asinhcor = round(r(rho),.01)
binscatter asinh_bg_stop_count asinh_hour_in_bg if city=="DENVER", text(0.2 5 "Corr.: `asinhcor'") scheme(s2color) ///
	 title("Denver") ytitle("") xtitle("") savegraph("bs_asinh_stop_hour_DENVER.gph") replace ylabel(0(1)7)

** NEWYORKCITY **
corr asinh_bg_stop_count asinh_hour_in_bg if city=="NEWYORKCITY"
local asinhcor = round(r(rho),.01)
binscatter asinh_bg_stop_count asinh_hour_in_bg if city=="NEWYORKCITY", text(0.2 5 "Corr.: `asinhcor'") scheme(s2color) ///
	 title("New York City") ytitle("") xtitle("") savegraph("bs_asinh_stop_hour_NEWYORKCITY.gph") replace ylabel(0(1)7)

** CHICAGO **
corr asinh_bg_stop_count asinh_hour_in_bg if city=="CHICAGO"
local asinhcor = round(r(rho),.01)
binscatter asinh_bg_stop_count asinh_hour_in_bg if city=="CHICAGO", text(0.2 5 "Corr.: `asinhcor'") scheme(s2color) ///
	 title("Chicago") ytitle("") xtitle("") savegraph("bs_asinh_stop_hour_Chicago.gph") replace ylabel(0(1)7)

** COLUMBUS **
corr asinh_bg_stop_count asinh_hour_in_bg if city=="COLUMBUS"
local asinhcor = round(r(rho),.01)
binscatter asinh_bg_stop_count asinh_hour_in_bg if city=="COLUMBUS", text(0.2 5 "Corr.: `asinhcor'") scheme(s2color) ///
	 title("Columbus") ytitle("") xtitle("") savegraph("bs_asinh_stop_hour_COLUMBUS.gph") replace ylabel(0(1)7)

** HOUSTON **
corr asinh_bg_stop_count asinh_hour_in_bg if city=="HOUSTON"
local asinhcor = round(r(rho),.01)
binscatter asinh_bg_stop_count asinh_hour_in_bg if city=="HOUSTON", text(0.2 5 "Corr.: `asinhcor'") scheme(s2color) ///
	 title("Houston") ytitle("") xtitle("") savegraph("bs_asinh_stop_hour_HOUSTON.gph") replace ylabel(0(1)7)

** NASHVILLE **
corr asinh_bg_stop_count asinh_hour_in_bg if city=="NASHVILLE"
local asinhcor = round(r(rho),.01)
binscatter asinh_bg_stop_count asinh_hour_in_bg if city=="NASHVILLE", text(0.2 5 "Corr.: `asinhcor'") scheme(s2color) ///
	 title("Nashville") ytitle("") xtitle("") savegraph("bs_asinh_stop_hour_NASHVILLE.gph") replace ylabel(0(1)7)

** OKLAHOMACITY **
corr asinh_bg_stop_count asinh_hour_in_bg if city=="OKLAHOMACITY"
local asinhcor = round(r(rho),.01)
binscatter asinh_bg_stop_count asinh_hour_in_bg if city=="OKLAHOMACITY", text(0.2 5 "Corr.: `asinhcor'") scheme(s2color) ///
	 title("Oklahoma City") ytitle("") xtitle("") savegraph("bs_asinh_stop_hour_OKLAHOMACITY.gph") replace ylabel(0(1)7)

** PHILADELPHIA **
corr asinh_bg_stop_count asinh_hour_in_bg if city=="PHILADELPHIA"
local asinhcor = round(r(rho),.01)
binscatter asinh_bg_stop_count asinh_hour_in_bg if city=="PHILADELPHIA", text(0.2 5 "Corr.: `asinhcor'") scheme(s2color) ///
	 title("Philadelphia") ytitle("") xtitle("") savegraph("bs_asinh_stop_hour_Philadelphia.gph") replace ylabel(0(1)7)

** SANANTONIO **
corr asinh_bg_stop_count asinh_hour_in_bg if city=="SANANTONIO"
local asinhcor = round(r(rho),.0001)
binscatter asinh_bg_stop_count asinh_hour_in_bg if city=="SANANTONIO", text(0.2 5 "Corr.: `asinhcor'") scheme(s2color) ///
	 title("San Antonio") ytitle("") xtitle("") savegraph("bs_asinh_stop_hour_SANANTONIO.gph") replace ylabel(0(1)7)

* Combine graphs *
graph combine bs_asinh_stop_hour_NEWYORKCITY.gph bs_asinh_stop_hour_Chicago.gph ///
	bs_asinh_stop_hour_Philadelphia.gph bs_asinh_stop_hour_COLUMBUS.gph ///
	bs_asinh_stop_hour_NASHVILLE.gph bs_asinh_stop_hour_HOUSTON.gph ///
	bs_asinh_stop_hour_SANANTONIO.gph bs_asinh_stop_hour_DENVER.gph bs_asinh_stop_hour_OKLAHOMACITY.gph, ///
	ycommon xcommon row(3) ysize(15) xsize(20)  ///
	b1("arsinh(Hour in BG)", size(small)) l1("arsinh(Number of Stops)", size(small)) iscale(*.7) imargin(small)
graph export "bs_asinh_stop_hour_9cities.png",replace width(4000)

local filelist: dir "${Output}/" files "bs_asinh_stop_hour_*.gph"
foreach f of local filelist{
 rm "${Output}/`f'"
}
************************************************************************
** Figure A7
************************************************************************
use "${DataCleaned}/police_hour_by_shift.dta", clear

****************************************
* Data preparation * 
****************************************
* merge with bg-level demographics
merge m:1 gidbg using "${master_dir}/DataRaw/acs13-17_bg_demographics.dta", nogenerate keep(master match) ///
keepusing(tot_population_acs_13_17 mail_return_rate_cen_2010 pct_nh_white_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_blk_alone_acs_13_17 pct_nh_asian_alone_acs_13_17 pct_college_acs_13_17 pct_prs_blw_pov_lev_acs_13_17 med_hhd_inc_bg_acs_13_17)

* transform variable to 0-1
local vars "pct_prs_blw_pov_lev_acs_13_17 mail_return_rate_cen_2010 pct_nh_white_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_blk_alone_acs_13_17 pct_nh_asian_alone_acs_13_17 pct_college_acs_13_17"
foreach var of local vars{
replace `var' = `var'/100
}

* change the unit of income 
replace med_hhd_inc_bg_acs_13_17 = med_hhd_inc_bg_acs_13_17 / 1000

* estimate disparity by shift hour 
set more off
estimates clear
forval j = 1/10{
reg asinh_hour_in_bg_hr`j' pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17, r absorb(city)
estimate store Hr_raw`j'
}

* plot black-white police presence disparity by shift hour 
coefplot (Hr_raw*, msize(medlarge) ciopts(lcolor(black))), ///
keep(pct_nh_blk_alone_acs_13_17) asequation swapnames vertical mfcolor(black) mlcolor(black) ///
	 coeflabels(, wrap(19)) scheme(s2color) graphregion(color(white)) ///
	mlabel format(%9.2g) mlabposition(12) mlabgap(*3) ytitle("Raw Disparity: % Black") ///
	eqrename(Hr_raw1 = "1" Hr_raw2 = "2" Hr_raw3 = "3" Hr_raw4 = "4" ///
	Hr_raw5 = "5" Hr_raw6 = "6" Hr_raw7 = "7" Hr_raw8 = "8" ///
	Hr_raw9 = "9" Hr_raw10 = "10" Hr_raw11 = "11" Hr_raw12 = "12" ) xtitle("Hour of the Shift")
graph export "${Output}/coefplot_raw_abs_black_disparity_by_shift_hr.png",replace	

* plot hispanic-white police presence disparity by shift hour 
coefplot (Hr_raw*, msize(medlarge) ciopts(lcolor(black))), ///
keep(pct_hispanic_acs_13_17) asequation swapnames vertical mfcolor(black) mlcolor(black) ///
	 coeflabels(, wrap(19)) scheme(s2color) graphregion(color(white)) ///
	mlabel format(%9.2g) mlabposition(12) mlabgap(*3) ytitle("Raw Disparity: % Hispanic") ///
	eqrename(Hr_raw1 = "1" Hr_raw2 = "2" Hr_raw3 = "3" Hr_raw4 = "4" ///
	Hr_raw5 = "5" Hr_raw6 = "6" Hr_raw7 = "7" Hr_raw8 = "8" ///
	Hr_raw9 = "9" Hr_raw10 = "10" Hr_raw11 = "11" Hr_raw12 = "12" ) xtitle("Hour of the Shift")
graph export "${Output}/coefplot_raw_abs_hisp_disparity_by_shift_hr.png",replace	

************************************************************************
** Figure A8
************************************************************************

use "${DataCleaned}/police_hour_data_for_reg.dta", clear

* collapse to city level 
sort city 
keep if city == city[_n-1]

keep sup_pct_black police_pct_black city

* generate city label 
gen city3 = proper(city)
replace city3 = "El Paso" if city3 == "Elpaso"
replace city3 = "Los Angeles" if city3 == "Losangeles"
replace city3 = "Oklahoma City" if city3 == "Oklahomacity"
replace city3 = "San Antonio" if city3== "Sanantonio"
replace city3 = "San Diego" if city3 == "Sandiego"
replace city3 = "San Francisco" if city3 == "Sanfrancisco"
replace city3 = "New York City" if city3 == "Newyorkcity"
replace city3 = "Fort Worth" if city3 == "Fortworth"

* create marker labels 
gen linevar = 0.1 * _n - 0.1
gen byte left_label = (city == "DETROIT" | city == "OKLAHOMACITY" | city == "NEWYORKCITY")
gen byte left_label_a1 = (city == "FORTWORTH" | city == "SANDIEGO" )

* Scatter plot: Supervisor % Black vs Police % Black	
tw (scatter sup_pct_black police_pct_black if left_label_a1 == 0,  scheme(s2color) graphregion(color(white)) ///
	mlabel(city3) mlabsize(vsmall) mcolor(navy) mlabcolor(navy) msize(small)  ///
	ytitle("Supervisor % Black") xtitle("Police Officer % Black")) ///
	(scatter sup_pct_black police_pct_black if left_label_a1 == 1,   ///
	mlabel(city3) mlabsize(vsmall) mcolor(navy) mlabcolor(navy) mlabpo(9) mlabgap(*1) msize(small) ) ///	
	(line linevar linevar if linevar <= 0.75, legend(off) xlabel(0(0.1)0.7) ylabel(0(0.1)0.7) lcolor(maroon)) 
graph export "${Output}/scatter_police_supervisor_black.png",replace
		
************************************************************************
** Figure A9
************************************************************************
set more off
use "${DataCleaned}/police_hour_data_for_reg.dta", clear

* run city-specific regression * 
* absolute share 
set more off
estimates clear
local cities "AUSTIN BOSTON CHARLOTTE CHICAGO COLUMBUS DALLAS DENVER DETROIT FORTWORTH HOUSTON INDIANAPOLIS LOSANGELES NASHVILLE NEWYORKCITY OKLAHOMACITY PHILADELPHIA PHOENIX SANANTONIO SANDIEGO SANFRANCISCO WASHINGTON"
foreach city of local cities{

	display "`city'"
	qui reg asinh_hour_in_bg pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 if city == "`city'", r   
	estimates store `city'_1
	
	qui reg asinh_hour_in_bg pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16 if city == "`city'", r 
	estimates store `city'_2
	
}

* Plot regression coefficients: mc_relative_black 
coefplot (*_1, pstyle(p2) msize(small) offset(0.25)) ///
		 (*_2, pstyle(p1) msymbol(D) msize(small) offset(-0.25)) , ///
keep(pct_nh_blk_alone_acs_13_17) asequation swapnames ///
 coeflabels(, wrap(19)) scheme(s2color) graphregion(color(white)) ///
mlabel format(%9.2g) mlabposition(12) mlabgap(*1) ytitle("")  ///
 xline(0, lcolor(black) lp(dot)) ysize(12) xsize(6)   ///
 eqrename(AUSTIN_1 = "Austin" AUSTIN_2 = "Austin" BOSTON_1 = "Boston" BOSTON_2 = "Boston" ///
	CHARLOTTE_1 = "Charlotte" CHARLOTTE_2 = "Charlotte" CHICAGO_1 = "Chicago" CHICAGO_2 = "Chicago" ///
	COLUMBUS_1 = "Columbus" COLUMBUS_2 = "Columbus" DALLAS_1 = "Dallas" DALLAS_2 = "Dallas" ///
	DENVER_1 = "Denver" DENVER_2 = "Denver" DETROIT_1 = "Detroit" DETROIT_2 = "Detroit" ///
	FORTWORTH_1 = "Fort Worth" FORTWORTH_2 = "Fort Worth" HOUSTON_1 = "Houston" HOUSTON_2 = "Houston" ///
	INDIANAPOLIS_1 = "Indianapolis" INDIANAPOLIS_2 = "Indianapolis" ///
	LOSANGELES_1 = "Los Angeles" LOSANGELES_2 = "Los Angeles" ///
	NASHVILLE_1 = "Nashville" NASHVILLE_2 = "Nashville" ///
	NEWYORKCITY_1 = "New York City" NEWYORKCITY_2 = "New York City" ///
	OKLAHOMACITY_1 = "Oklahoma City" OKLAHOMACITY_2 = "Oklahoma City" ///
	PHILADELPHIA_1 = "Philadelphia" PHILADELPHIA_2 = "Philadelphia" ///
	PHOENIX_1 = "Phoenix" PHOENIX_2 = "Phoenix" ///
	SANANTONIO_1 = "San Antonio" SANANTONIO_2 = "San Antonio" ///
	SANDIEGO_1 = "San Diego" SANDIEGO_2 = "San Diego" ///
	SANFRANCISCO_1 = "San Francisco" SANFRANCISCO_2 = "San Francisco" ///
	WASHINGTON_1 = "Washington" WASHINGTON_2 = "Washington" ) ///
	legend(order(2 "No Control" 4 "With Controls")) ///
	xtitle("% Black")	
graph export "${Output}/coefplot_raw_black_by_city.png",replace  

***************************************************************************************************
** 2. Appendix Tables
***************************************************************************************************
			 
************************************************************************
** Table A1 
************************************************************************

use "${DataCleaned}/phones_LEMAS_LEOKA_validate.dta", clear

********************************
* Table A.1: control for city racial composition
********************************

reg police_pct_white phones_white_17 city_pctnh_white_alone_acs_13_17, r
outreg2 using "${Output}/reg_LEMAS_comparison.tex", replace alpha(0.01, 0.05, 0.1) label nonotes tex(frag)

reg police_pct_black phones_black_17 city_pctnh_blk_alone_acs_13_17, r
outreg2 using "${Output}/reg_LEMAS_comparison.tex", append  alpha(0.01, 0.05, 0.1) label nonotes tex(frag)

reg police_pct_hisp phones_hisp_17 city_pcthisp_latino_acs_13_17, r
outreg2 using "${Output}/reg_LEMAS_comparison.tex", append  alpha(0.01, 0.05, 0.1) label nonotes tex(frag)

reg police_pct_asian phones_asian_17 city_pctnh_asian_alone_acs_13_17, r
outreg2 using "${Output}/reg_LEMAS_comparison.tex", append  alpha(0.01, 0.05, 0.1) label nonotes tex(frag)

************************************************************************
** Table A.2: Summary stats
************************************************************************

use "${DataCleaned}/police_hour_data_for_reg.dta", clear

cd "${Output}/"
sutex2 hour_in_bg asinh_hour_in_bg N_shifts_in_bg asinh_N_shifts_in_bg  ///
 pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17  ///
 tot_population_acs_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 ///
 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16, ///
 sav("summary_table.tex") minmax tabular varlabel replace 

************************************************************************
** Table A.3: Use 2013-2016 homicide measure
************************************************************************

use "${DataCleaned}/police_hour_data_for_reg.dta", clear

reg asinh_hour_in_bg mc_pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 , r   
outreg2 using "${Output}/reg_13-16homicide_w_NYC_abs.tex",replace  nocons nonotes tex(frag) addtext(City FE, No) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

reg asinh_hour_in_bg mc_pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17, r absorb(city)
outreg2 using "${Output}/reg_13-16homicide_w_NYC_abs.tex", append nocons nonotes tex(frag) addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

reg asinh_hour_in_bg mc_pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 homicide_count_13_16_avg km_to_nearest_homicide_min_13_16 , r absorb(city) 
outreg2 using "${Output}/reg_13-16homicide_w_NYC_abs.tex", append nocons nonotes tex(frag) addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

* note: do NOT add mc_sup_pct_black or mc_pct_blk in the regression because City FE is added
reg asinh_hour_in_bg mc_pct_nh_blk_alone_acs_13_17 mc_pct_blk_X_police_blk pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17  ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 homicide_count_13_16_avg km_to_nearest_homicide_min_13_16 , r absorb(city)
outreg2 using "${Output}/reg_13-16homicide_w_NYC_abs.tex", append nocons nonotes tex(frag) addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

reg asinh_hour_in_bg mc_pct_nh_blk_alone_acs_13_17 mc_pct_blk_X_police_blk mc_pct_blk_X_sup_blk pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17  ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 homicide_count_13_16_avg km_to_nearest_homicide_min_13_16 , r absorb(city)
outreg2 using "${Output}/reg_13-16homicide_w_NYC_abs.tex", append nocons nonotes tex(frag) addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

*************************************************
* Table A4: Use 311 calls as control for demand for service
*************************************************
use "${DataCleaned}/police_hour_data_for_reg.dta", clear

keep if city == "NEWYORKCITY"

merge 1:1 gidbg using "${DataRaw}/311_calls_bg_count_NYC_2016.dta", keep(master match) 

* fill in zero (_merge == 1 means zero calls)
set more off
foreach var of varlist tot_311_calls tot_311_calls_NYPD tot_311_calls_HPD tot_311_calls_DOT tot_311_calls_DEP tot_311_calls_DSNY tot_311_calls_DOB tot_311_calls_DPR tot_311_calls_DOHMH tot_311_calls_DHS{
	replace `var' = 0 if missing(`var') & _merge == 1 // 9 observations _merge == 1
}

drop _merge

* asinh transformation
foreach var of varlist tot_311_calls tot_311_calls_NYPD tot_311_calls_HPD tot_311_calls_DOT tot_311_calls_DEP tot_311_calls_DSNY tot_311_calls_DOB tot_311_calls_DPR tot_311_calls_DOHMH tot_311_calls_DHS{
	gen asinh_`var' = asinh(`var')
}

* label variables 
label variable tot_311_calls "N. Total 311 Calls"
label variable asinh_tot_311_calls "asinh(Total 311 Calls)"
local agencies "NYPD HPD DOT DEP DSNY DOB DPR DOHMH DHS"
foreach agency of local agencies{
	label variable  tot_311_calls_`agency' "N. 311 Calls - `agency'"
	label variable  asinh_tot_311_calls_`agency' "asinh(311 Calls - `agency')"
}

* regression: controling for number of 311 calls
reg asinh_hour_in_bg pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17, r   
outreg2 using "${Output}/reg_hour_311_calls_NYC_abs_race.tex",replace  nocons nonotes tex(frag) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

reg asinh_hour_in_bg pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16, r 
outreg2 using "${Output}/reg_hour_311_calls_NYC_abs_race.tex", append nocons nonotes tex(frag) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

reg asinh_hour_in_bg pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16 asinh_tot_311_calls, r 
outreg2 using "${Output}/reg_hour_311_calls_NYC_abs_race.tex", append nocons nonotes tex(frag) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

reg asinh_hour_in_bg pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16 asinh_tot_311_calls_NYPD, r 
outreg2 using "${Output}/reg_hour_311_calls_NYC_abs_race.tex", append nocons nonotes tex(frag) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

reg asinh_hour_in_bg pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16 ///
	asinh_tot_311_calls_NYPD asinh_tot_311_calls_HPD asinh_tot_311_calls_DOT asinh_tot_311_calls_DEP ///
	asinh_tot_311_calls_DSNY asinh_tot_311_calls_DOB asinh_tot_311_calls_DPR asinh_tot_311_calls_DOHMH asinh_tot_311_calls_DHS, r
outreg2 using "${Output}/reg_hour_311_calls_NYC_abs_race.tex", append nocons nonotes tex(frag)  alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

*************************************************
* Table A5: estiamte correlates of police hours outside weekday 9am-5pm
*************************************************

use "${DataCleaned}/police_hour_data_for_reg_nonwork.dta", clear

reg asinh_hour_in_bg mc_pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 , r   
outreg2 using "${Output}/asinh_hour_race_nonwork.tex",replace  nocons nonotes tex(frag) addtext(City FE, No) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

reg asinh_hour_in_bg mc_pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 , r  absorb(city)
outreg2 using "${Output}/asinh_hour_race_nonwork.tex", append nocons nonotes tex(frag) addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label	

reg asinh_hour_in_bg mc_pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17  ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16, r absorb(city) 
outreg2 using "${Output}/asinh_hour_race_nonwork.tex", append nocons nonotes tex(frag) addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

* note: do NOT add mc_sup_pct_black or mc_pct_blk in the regression because city FE is added
reg asinh_hour_in_bg mc_pct_nh_blk_alone_acs_13_17 mc_pct_blk_X_police_blk pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16, r absorb(city)
outreg2 using "${Output}/asinh_hour_race_nonwork.tex", append nocons nonotes tex(frag) addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

reg asinh_hour_in_bg mc_pct_nh_blk_alone_acs_13_17 mc_pct_blk_X_police_blk mc_pct_blk_X_sup_blk pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17  ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16, r absorb(city)
outreg2 using "${Output}/asinh_hour_race_nonwork.tex", append nocons nonotes tex(frag) addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

************************************************************************
** Appendix Table 6: drop precincts with foot traffic
************************************************************************

use "${DataCleaned}/police_hour_data_for_reg.dta", clear

keep if city=="NEWYORKCITY"

* raw + residual
reg asinh_hour_in_bg pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17, r  
outreg2 using "${Output}/asinh_hour_raw_race_NYC_4col.tex",replace  nocons nonotes tex(frag) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

reg asinh_hour_in_bg pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16, r  
outreg2 using "${Output}/asinh_hour_raw_race_NYC_4col.tex", append nocons nonotes tex(frag) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

* drop precincts
drop if district_ID == 1 | district_ID == 6 | district_ID == 14 | district_ID == 18 | district_ID == 8

reg asinh_hour_in_bg pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17, r
outreg2 using "${Output}/asinh_hour_raw_race_NYC_4col.tex", append nocons nonotes tex(frag) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

reg asinh_hour_in_bg pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16, r  
outreg2 using "${Output}/asinh_hour_raw_race_NYC_4col.tex", append nocons nonotes tex(frag) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) label

************************************************************************
** Appendix Table 7
************************************************************************

use "${DataCleaned}/bg_stop_arrest_per_hour_for_reg.dta", clear

reg asinh_hour_in_bg pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 if ~missing(asinh_bg_stop_count), r absorb(city) 
outreg2 using "${Output}/asinh_conditional_stops_abs_race.tex",replace  nocons nonotes addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) tex(frag) label
reg asinh_bg_stop_count pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 if ~missing(asinh_bg_stop_count), r absorb(city)
outreg2 using "${Output}/asinh_conditional_stops_abs_race.tex", append nocons nonotes addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) tex(frag) label
reg asinh_stop_to_hour pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 if ~missing(asinh_bg_stop_count), r absorb(city) 
outreg2 using "${Output}/asinh_conditional_stops_abs_race.tex", append nocons nonotes addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) tex(frag) label

reg asinh_hour_in_bg pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16 if ~missing(asinh_bg_stop_count), r absorb(city)
outreg2 using "${Output}/asinh_conditional_stops_abs_race.tex", append nocons nonotes addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) tex(frag) label
reg asinh_bg_stop_count pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16 if ~missing(asinh_bg_stop_count), r absorb(city)  
outreg2 using "${Output}/asinh_conditional_stops_abs_race.tex", append nocons nonotes addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) tex(frag) label
reg asinh_stop_to_hour pct_nh_blk_alone_acs_13_17 pct_hispanic_acs_13_17 pct_nh_asian_alone_acs_13_17 ln_population_13_17 pct_college_acs_13_17 med_hhd_inc_bg_acs_13_17 mail_return_rate_cen_2010 km_to_nearest_homicide16 homicide_count_16 if ~missing(asinh_bg_stop_count), r absorb(city)  
outreg2 using "${Output}/asinh_conditional_stops_abs_race.tex", append nocons nonotes addtext(City FE, Yes) alpha(0.001, 0.01, 0.05, 0.1) symbol(***, **, *, +) tex(frag) label


************************************************************************
** Statistics in the paper 
************************************************************************

use "${DataCleaned}/phones_LEMAS_LEOKA_validate.dta", clear

* testing slope 
reg police_pct_white phones_white_17, r
test phones_white == 1  
reg police_pct_black phones_black_17, r
test phones_black == 1 
reg police_pct_hisp phones_hisp_17, r
test phones_hisp == 1  
reg police_pct_asian phones_asian_17, r
test phones_asian == 1 

